package com.example.semaphore;

import java.util.concurrent.Semaphore;

public class SemaphoreMain {

    Semaphore semaphore = new Semaphore(3);

    public static void main(String[] args) {
        SemaphoreMain semaphoreMain = new SemaphoreMain();
        for (int i=0;i<10;i++){
            new Thread(()->{
                try {
                    semaphoreMain.semaphore.acquire();
                    System.out.println("线程:"+Thread.currentThread().getName());
                    Thread.sleep(3000);
                    //semaphoreMain.semaphore.release();
                    System.out.println(semaphoreMain.semaphore.availablePermits()+"---"+Thread.currentThread().getName());
                    System.out.println("执行完成"+Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("释放信号");
        semaphoreMain.semaphore.release();

    }


}
